package com.cmower.dzone.optional;

import java.util.Optional;

public class ifPresentOptionalDemo {
    public static void main(String[] args) {
Optional<String> optOrNull = Optional.ofNullable(null);
if (optOrNull.isPresent()) {
    System.out.println(optOrNull.get().length());
}

        Optional<String> opt = Optional.of("沉默王二");
        opt.ifPresent(str -> System.out.println(str.length()));

        opt.ifPresentOrElse(str -> System.out.println(str.length()),() -> System.out.println("为空"));
    }
}
